<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<script th:src="@{/static/js/jquery.min.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/js/afquery.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/layui/layui.all.js}" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}" />
		<link rel="stylesheet" type="text/css" th:href="@{/static/css/Common.css}" />
	</head>
	<body>
		<!-- 对话框面板定义 -->
		<div class='student_dlg_define' style='display:none'>
			<div class='pane' style='padding:16px'>
				<div style='padding:10px'>
					<span>按学号/工号搜索:</span>
					<input class='userID' type='text' style='width:100%' placeholder="请输入学号或工号">
				</div>
				<div style='padding:10px'>
					<span>按学院搜索:</span>
					 <select class="academyID">
						 <option></option>
						<option th:each ="row : ${Academy}" th:text="${row.AcademyId}"></option>
					 </select>
				</div>
				<div style='padding:10px'>
					<span>按班级搜索:</span>
					<input class='clazz' type='text' style='width:100%'>
				</div>
			</div>
		</div>
		<!-- 对话框面板定义 -->
		<div class='teacher_dlg_define' style='display:none'>
			<div class='pane' style='padding:16px'>
				<div style='padding:10px'>
					<span>按学号/工号搜索:</span>
					<input class='userID' type='text' style='width:100%' placeholder="请输入学号或工号">
				</div>
				<div style='padding:10px'>
					<span>按学院搜索:</span>
					 <select class="academyID" >
						 <option></option>
						<option th:each ="row : ${Academy}" th:text="${row.AcademyId}"></option>
					 </select>
				</div>
			</div>
		</div>
		<div class="layui-row" id="Main" style="height: 100%;overflow: hidden;">
			<!-- 顶部区 -->
			<div th:replace=" ~{A_Common::Top_Menu}"></div>

			<!-- 左侧菜单区 -->
			<div th:replace=" ~{A_Common::Left_Menu}"></div>

			<!-- 右侧内容区 -->
			<!-- ==========================================人员管理================================================ -->
			<div th:fragment="User_Manager" class="layui-col-xs8 layui-col-md10 layui-row" id="right_Content">
				<fieldset class="layui-elem-field site-demo-button" style="padding: 5%;">
					<legend>录入人员信息</legend>
					<div class="layui-tab">
						<ul class="layui-tab-title">
							<li onclick="M.tab(this)">新增人员</li>
							<li onclick="M.tab(this)">教师管理</li>
							<li onclick="M.tab(this)">学生管理</li>
						</ul>
						<div class="layui-tab-content">
							<div class="layui-tab-item">
								<div class="layui-form" action="">
									<div class="layui-form-item">
										<div class="layui-inline">
											<label class="layui-form-label">账号</label>
											<div class="layui-input-inline">
												<input type="text" placeholder="学号/工号" class="layui-input">
											</div>
										</div>
										<div class="layui-inline">
											<label class="layui-form-label">密码</label>
											<div class="layui-input-inline">
												<input type="text" class="layui-input" value="123456" disabled="disabled">

											</div>

										</div>
										<div class="layui-inline" onclick="M.OpenPassword()">
											<input type="checkbox" id="open" lay-skin="switch" lay-text="开启|关闭">
										</div>
										<div class="layui-inline">
											<label class="layui-form-label">姓名</label>
											<div class="layui-input-inline">
												<input type="text" class="layui-input">
											</div>
										</div>
									</div>
									<div class="layui-form-item">
										<div class="layui-inline">
											<label class="layui-form-label">性别</label>
											<div class="layui-input-block">
												<input type="radio" name="sex" value="true" title="男" checked="">
												<input type="radio" name="sex" value="false" title="女">
											</div>
										</div>
									</div>
									<div class="layui-form-item">
										<div class="layui-input-block">
											<button class="layui-btn" onclick="M.save()">立即提交</button>
											<button type="reset" class="layui-btn layui-btn-primary">重置</button>
										</div>
									</div>
								</div>
							</div>
							<div class="layui-tab-item" id="teacherTable">
								<table class="layui-table">
									<colgroup>
										<col>
									</colgroup>
									<thead>
										<tr>
											<th><input type="checkbox" onclick="M.SelectAll(this)" /></th>
											<th>序号</th>
											<th>姓名</th>
											<th>性别</th>
											<th>学院</th>
											<th>工号</th>
											<th>密码</th>
											<th>管理员</th>
										</tr>
									</thead>
									<tbody>
										<tr th:each = "row : ${TeacherList}" >
											<td><input type="checkbox" name="selectID" /></td>
											<td th:idd="${row.id}" th:isDean="${row.isDean}" th:text="${rowStat.index}+1">1</td>
											<td th:text = "${row.realName}">阿里巴巴</td>
											<td>
												<select disabled="disabled" th:if="${row.Sex == true}">
													<option value="true" selected="selected">男</option>
													<option value="false">女</option>
												</select>
												<select disabled="disabled" th:if="${row.Sex == false}">
													<option value="true">男</option>
													<option value="false" selected="selected">女</option>
												</select>
											</td>
											<td>
												<select disabled="disabled">
													<option></option>
													<option th:each = "rows : ${Academy}" th:text="${rows.academyName}" th:value="${rows.academyId}"></option>
												</select>
											</td>
											<td th:text = "${row.teacherId}">500223</td>
											<td th:text = "${row.password}">123456</td>
											<td>
												<select disabled="disabled" th:if="${row.isManager == true}">
													<option value="true" selected="selected" style="color: yellow;">是</option>
													<option value="false">否</option>
												</select>
												<select disabled="disabled" th:if="${row.isManager == false}">
													<option value="true" style="color: yellow;">是</option>
													<option value="false" selected="selected">否</option>
												</select>
											</td>
										</tr>
										<tr th:each = "row : ${TeacherASList}">
											<td><input type="checkbox" name="selectID" /></td>
											<td th:oldAcademyId="${row.AcademyId}" th:idd="${row.id}" th:isDean="${row.isDean}" th:text="${rowStat.index}+${TeacherCount}+1">1</td>
											<td th:text = "${row.RealName}">阿里巴巴</td>
											<td>
												<select disabled="disabled" th:if="${row.Sex == true}">
													<option value="true" selected="selected">男</option>
													<option value="false">女</option>
												</select>
												<select disabled="disabled" th:if="${row.Sex == false}">
													<option value="true">男</option>
													<option value="false" selected="selected">女</option>
												</select>
											</td>
											<td>
												<select disabled="disabled">
													<option></option>
													<option th:each = "rows : ${Academy}" th:text="${rows.academyName}"
													 th:value="${rows.academyId}" th:selected="${rows.academyName == row.AcademyName}" ></option>
												</select>
											</td>
											<td th:text = "${row.TeacherId}">500223</td>
											<td th:text = "${row.Password}">123456</td>
											<td>
												<select disabled="disabled" th:if="${row.isManager == true}">
													<option value="true" selected="selected" style="color: yellow;">是</option>
													<option value="false">否</option>
												</select>
												<select disabled="disabled" th:if="${row.isManager == false}">
													<option value="true" style="color: yellow;">是</option>
													<option value="false" selected="selected">否</option>
												</select>
											</td>
										</tr>
									
									</tbody>
								</table>
<!-- 								<div class="nothing">
									没有查询到有用数据
								</div> -->
								<div class="layui-row">
									<div class="layui-btn-group layui-col-md-offset7">
										<button type="button" class="layui-btn" onclick="selectTeacher()">搜索</button>
										<button type="button" class="layui-btn" onclick="M.OpenUpdate(this)">编辑</button>
										<button type="button" class="layui-btn" onclick="M.DeleteModel(this)">删除</button>
										<button type="button" class="layui-btn" onclick="M.SaveUpdate(this)">保存</button>
									</div>
								</div>
							</div>
							<div class="layui-tab-item" id="studentTable">
								<table class="layui-table">
									<colgroup>
										<col>
									</colgroup>
									<thead>
										<tr>
											<th><input type="checkbox" onclick="M.SelectAll(this)" /></th>
											<th>序号</th>
											<th>姓名</th>
											<th>性别</th>
											<th>学院</th>
											<th>学号</th>
											<th>密码</th>
											<th>班级号</th>
										</tr>
									</thead>
									<tbody>
										<tr th:each = "row : ${A_C_StudentList}">
											<td><input type="checkbox" name="selectID" /></td>
											<td th:idd="${row.id}" th:text="${rowStat.index}+1" >1</td>
											<td th:text = "${row.RealName}">阿里巴巴</td>
											<td>
												<select disabled="disabled" th:if="${row.Sex == true}">
													<option value="true" selected="selected">男</option>
													<option value="false">女</option>
												</select>
												<select disabled="disabled" th:if="${row.Sex == false}">
													<option value="true">男</option>
													<option value="false" selected="selected">女</option>
												</select>
											</td>
											<td th:text = "${row.AcademyName}">软件学院</td>
											<td th:text = "${row.StudentId}">500223</td>
											<td th:text = "${row.Password}">123456</td>
											<td th:text = "${row.ClazzId}">123456</td>
										</tr>
									</tbody>
								</table>
<!-- 								<div class="nothing">
									没有查询到有用数据
								</div> -->
								<div class="layui-row">
									<div class="layui-btn-group layui-col-md-offset7">
										<button type="button" class="layui-btn" onclick="selectStudent()">搜索</button>
										<button type="button" class="layui-btn" onclick="M.OpenUpdate(this)">编辑</button>
										<button type="button" class="layui-btn" onclick="M.DeleteModel(this)">删除</button>
										<button type="button" class="layui-btn" onclick="M.SaveUpdate(this)">保存</button>
									</div>
								</div>
							</div>
						</div>
				</fieldset>
			</div>
		</div>



	</body>
	<script th:replace=" ~{A_Common::Init_Click}"></script>
	<script th:inline="javascript">
		
			var ClazzList = [[${ClazzList}]];
			var Academy = [[${Academy}]];
			var ss = [[${TeacherASList}]];
			var sss = [[${TeacherList}]];
			// var M = {};
		function selectStudent(){
					
					var dlgContent = $('.student_dlg_define').html(); // 窗口内容
					
					layer.open({
						type: 1,
						title: '添加记录', 
						area: ['420px', 'auto'], 
						content: dlgContent,
						btn:['确定','放弃'] ,
						
						btn1: function(index,layero){
							let userID = $('.userID', layero).val().trim();
							let academyID = $('.academyID', layero).val().trim();
							let clazz = $('.clazz', layero).val().trim();
							if(userID.length != 0 && academyID.length == 0 && clazz.length == 0){
								M.getStudents(Number(userID),0,0);
							}else if(userID.length == 0 && academyID.length != 0 && clazz.length == 0){
								M.getStudents(0,academyID,0);
							}else if(userID.length == 0 && academyID.length == 0 && clazz.length != 0){
								M.getStudents(0,0,Number(clazz));
							}else{
								layer.open({
									title: "信息",
									content: '请只输入一个查询条件',
									offset: 't',
									time: 2000
								});
							}
							
							// 关闭对话框
							layer.close(index)	;				
						},
						btn2 : function(index,layero){
							layer.close(index)		
						}
					});	
					
				}
		function selectTeacher(){
					
					var dlgContent = $('.teacher_dlg_define').html(); // 窗口内容
					
					layer.open({
						type: 1,
						title: '添加记录', 
						area: ['420px', 'auto'], 
						content: dlgContent,
						btn:['确定','放弃'] ,
						
						btn1: function(index,layero){
							let userID = $('.userID', layero).val().trim();
							let academyID = $('.academyID', layero).val().trim();
							if(userID.length != 0 && academyID.length == 0){
								M.getTeachers(Number(userID),0);
							}else if(userID.length == 0 && academyID.length != 0){
								M.getTeachers(0,academyID);
							}else{
								layer.open({
									title: "信息",
									content: '请只输入一个查询条件',
									offset: 't',
									time: 2000
								});
							}
							
							// 关闭对话框
							layer.close(index)	;				
						},
						btn2 : function(index,layero){
							layer.close(index)		
						}
					});	
					
				}	
		
		//Demo
		layui.use('form', function() {
			var form = layui.form;
			form.render();
			//监听提交
			form.on('submit(formDemo)', function(data) {
				layer.msg(JSON.stringify(data.field));
				return false;
			});
		});
		

		// 允许修改密码
		M.OpenPassword = function() {
			if ($("#open").is(':checked')) {
				$($(".layui-input").get(1)).attr("disabled", false);
			} else {
				$($(".layui-input").get(1)).attr("disabled", true);

				$($(".layui-input").get(1)).val($($(".layui-input").get(1)).val().trim());

			}

		}
		
		//保存教师/学生信息
		M.save = function() {
			
			var isExist = true;
			var req = {};
			req.userId = $($(".layui-input").get(0)).val().trim();
			req.Password = $($(".layui-input").get(1)).val().trim();
			req.RealName = $($(".layui-input").get(2)).val().trim();
			req.Sex = $('input:radio:checked').val();
			
			if (req.userId.length == 9 && req.Password.length >= 6) {
				
				for(let i = 0; i<ClazzList.length;i++){
					if(Number(req.userId.slice(0,7)) == ClazzList[i].clazzId){
						req.ClazzId = ClazzList[i].clazzId;
						isExist = false;
						break;
					}
				}
				if(isExist){
					layer.open({
						title: "信息",
						content: '无效的账号',
						offset: 't',
						time: 2000
					});
					return;
				}
				
				req.StudentId = Number(req.userId);
				Af.rest([[@{index_saveStudent.do}]], req, function(data) {
					layer.open({
						title: "信息",
						content: '插入成功',
						offset: 't',
						time: 500,
						success: function(layero, index){
							setTimeout(function(){location.reload();},600);
						  } 
					});
					$($(".layui-input").get(0)).val("");
					$($(".layui-input").get(2)).val("");
				});
			} else if (req.userId.length == 6 && req.Password.length >= 6) {
				req.TeacherId = Number(req.userId);
				Af.rest([[@{index_saveTeacher.do}]], req, function(data) {
					layer.open({
						title: "信息",
						content: '插入成功',
						offset: 't',
						time: 500,
						success: function(layero, index){
							setTimeout(function(){location.reload();},600);
						  } 
					});
					$($(".layui-input").get(0)).val("");
					$($(".layui-input").get(2)).val("");
				});
			} else {
				layer.open({
					title: "信息",
					content: '请检查账号格式',
					offset: 't',
					time: 2000
				});
			}
		}
		
		//修改信息
		var count = 0;
		M.OpenUpdate = function(e) {

			let tables = $($(e).parent().parent()).prev();
			let id = $($(e).parent().parent().parent()).attr("id");
			let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
			count = checkeds.length;
			if (checkeds.length > 0) {
				$(checkeds.parent().parent()).css("background-color","khaki");
				$($(checkeds).parent().parent()).attr("contenteditable", true);
				let trs = $(checkeds).parent().parent();
				for (let i = 0; i < checkeds.length; i++) {
					$($($(trs.get(i)).children()).get(0)).attr("contenteditable", false);
					$($($(trs.get(i)).children()).get(1)).attr("contenteditable", false);
					$($($(trs.get(i)).children()).get(3)).attr("contenteditable", false);
					$($(trs.get(i)).find("td select")).prop("disabled",false);
				}
				if(id == "studentTable"){
					$($($(trs).children()).get(4)).attr("contenteditable", false);
				}
			}else{
				layer.open({
					title: "信息",
					content: '请选择需要修改的数据！',
					offset: 't',
					time: 2000
				});
			}
			
		}


		//保存修改后信息
		M.SaveUpdate = function(e) {

			let tables = $($(e).parent().parent()).prev();
			let trs = $(tables).find("tbody tr");
			
			let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
			let datas = new Array();
			if (checkeds.length != count) {
				layer.open({
					title: "信息",
					content: '请检查修改的数据数量！',
					offset: 't',
					time: 2000
				});
			} else {
				let tableId = $($(e).parent().parent().parent()).attr("id");
				if(tableId == "teacherTable"){
					for (let i = 0; i < checkeds.length; i++) {
						var req = {};
						req.id = Number($($($($(checkeds[i]).parent().parent()).children()).get(1)).attr("idd"));
						req.RealName = $($($($(checkeds[i]).parent().parent()).children()).get(2)).text().trim();
						req.Sex = $($($(checkeds[i]).parent().parent()).find("td select:eq(0) option:selected")).val();
						req.TeacherId = Number($($($($(checkeds[i]).parent().parent()).children()).get(5)).text().trim());
						req.AcademyId = $($($(checkeds[i]).parent().parent()).find("td select:eq(1) option:selected")).val();
						req.Password = $($($($(checkeds[i]).parent().parent()).children()).get(6)).text().trim();
						req.isManager = $($($(checkeds[i]).parent().parent()).find("td select:eq(2) option:selected")).val();
						req.isDean = $($($($(checkeds[i]).parent().parent()).children()).get(1)).attr("isDean");
						req.oldAcademyId = Number($($($($(checkeds[i]).parent().parent()).children()).get(1)).attr("oldAcademyId"));
							if(req.RealName == "" || req.TeacherId == "" || req.Password == ""){
								layer.open({
									title: "信息",
									content: '不能输入空值！',
									offset: 't',
									time: 2000
								});
								return;
							}else if(String(req.TeacherId).length != 6 || req.Password.length < 6){
								layer.open({
									title: "信息",
									content: '请输入6位工号和大于6位的密码！',
									offset: 't',
									time: 2000
								});
								return;
							}
						datas.push(req);
						}
						Af.rest([[@{index_saveUpdateTeacher.do}]], datas, function(data) {
							layer.open({
								title: "信息",
								content: '成功！',
								offset: 't',
								time: 500
							});
							// location.reload();
						});
					}else{
						for (let i = 0; i < checkeds.length; i++) {
							var req = {};
							req.RealName = $($($($(checkeds[i]).parent().parent()).children()).get(2)).text().trim();
							req.Sex = $($($(checkeds[i]).parent().parent()).find("td select option:selected")).val();
							req.id = Number($($($($(checkeds[i]).parent().parent()).children()).get(1)).attr("idd"));
							req.StudentId = Number($($($($(checkeds[i]).parent().parent()).children()).get(5)).text().trim());
							req.Password = $($($($(checkeds[i]).parent().parent()).children()).get(6)).text().trim();
							req.ClazzId = $($($($(checkeds[i]).parent().parent()).children()).get(7)).text().trim();
	
							if(req.RealName == "" || req.StudentId == "" || req.Password == "" ||req.ClazzId == ""){
								layer.open({
									title: "信息",
									content: '不能输入空值！',
									offset: 't',
									time: 2000
								});
								return;
							}else if(String(req.StudentId).length != 9 || req.Password.length < 6 || String(req.ClazzId).length != 7){
								layer.open({
									title: "信息",
									content: '请输入9位学号、大于6位的密码和7位的班级号！',
									offset: 't',
									time: 2000
								});
								return;
							}
							for(let j=0;j<ClazzList.length;j++){
								if(req.ClazzId == ClazzList[i].clazzId){
									break;
								}else{
									layer.open({
										title: "信息",
										content: '无效的班级！',
										offset: 't',
										time: 2000
									});
									return;
								}
								
							}
							datas.push(req);
						}
						Af.rest([[@{index_saveUpdateStudent.do}]], datas, function(data) {
							layer.open({
								title: "信息",
								content: '成功！',
								offset: 't',
								time: 500
							});
						});
					}
					$(trs).attr("contenteditable", false);
					$(trs.find("td select")).prop("disabled",true);
					$($(tables).find("tbody tr td input")).prop("checked", false);
					$(checkeds.parent().parent()).css("background-color","white");
					count = 0;
				}
				
			
		}
		
		// 删除一条记录
		M.DeleteModel = function(e){
			let tables = $($(e).parent().parent()).prev();

			let id = $($(e).parent().parent().parent()).attr("id");
			let checkeds = tables.find("tbody tr td input[name='selectID']:checked");
			if (checkeds.length > 0) {
				let datas = new Array();
				for (let i = 0; i < checkeds.length; i++) {
					let s = $($(checkeds.get(i)).parent()).next().attr("idd");
					
					datas.push(Number(s));
				}
				
				if(id == "teacherTable"){
					Af.rest([[@{index_deleteTeacher.do}]],datas,function(data){
						layer.open({
							title: "信息",
							content: '成功！',
							offset: 't',
							time: 500,
							success: function(layero, index){
								setTimeout(function(){location.reload();},600);
							  }
						});
						
					});
				}else{
					Af.rest([[@{index_deleteStudent.do}]],datas,function(data){
						layer.open({
							title: "信息",
							content: '成功！',
							offset: 't',
							time: 500,
							success: function(layero, index){
								setTimeout(function(){location.reload();},600);
							  }
						});
					});
				}
				
				
			}
		}

		// 全选按钮
		M.SelectAll = function(e) {

			if ($(e).is(':checked')) {
				let tables = $(e).parent().parent().parent().parent();
				$($(tables).find("tbody tr td input")).prop("checked", true);
			} else {
				let tables = $(e).parent().parent().parent().parent();
				$($(tables).find("tbody tr td input")).prop("checked", false);

			}
		}
		
		var page = sessionStorage.getItem("page");
		M.init = function(){
			$(tab_titles).removeAttr("class");
			$(tab_contents).hide();
			if(Number(page) >= 0){
				$(tab_titles).removeAttr("class");
				$(tab_contents).hide();
				
				switch(Number(page)){
					case 0:
						sessionStorage.setItem("page",0);
						$(".layui-tab-title li:eq(0)").attr("class","layui-this");
						$(".layui-tab-content .layui-tab-item:eq(0)").show();
					break;
					case 1:
						sessionStorage.setItem("page",1);
						var s = $(".layui-tab-title li:eq(1)").attr("class","layui-this");
						var m = $(".layui-tab-content .layui-tab-item:eq(1)").show();
					break;
					case 2:
						sessionStorage.setItem("page",2);
						var s = $(".layui-tab-title li:eq(2)").attr("class","layui-this");
						var m = $(".layui-tab-content .layui-tab-item:eq(2)").show();
					break;
					default: 
					       sessionStorage.setItem("page",0);
					       $(".layui-tab-title li:eq(0)").attr("class","layui-this");
					       $(".layui-tab-content .layui-tab-item:eq(0)").show();
					break;
				}	
			}else{
				sessionStorage.setItem("page",0);
				$(".layui-tab-title li:eq(0)").attr("class","layui-this");
				$(".layui-tab-content .layui-tab-item:eq(0)").show();
				}
			}();
		
	</script>



	
</html>
